package com.ce.yw.leaveflow.action;

import java.util.Date;
import java.util.List;

import org.activiti.engine.task.Task;

import com.ce.framework.constants.WebConstants;
import com.ce.framework.exception.AppException;
import com.ce.framework.util.BeanAccessUtil;
import com.ce.pub.process.action.ActivitiBaseAction;
import com.ce.sys.login.model.User;
import com.ce.yw.leaveflow.logic.search.OaLeaveSearchCondition;
import com.ce.yw.leaveflow.view.OaLeaveView;

/**
 * 嫦娥软件有限公司 版权所有 2013 
 * 创建人：　张波 
 * 创建日期：2014-01-27 
 * 创建时间：14:17:44 
 * 功能描述：请假
 * [OA_LEAVE]更改时用的Action
 *  ============================================== 
 * 修改历史 
 * 修改人
 * 修改时间 
 * 修改原因
 * ==============================================
 */
public class OaLeaveAction extends ActivitiBaseAction {
	private static final long serialVersionUID = 1L;

	private String opMenu;

	@SuppressWarnings("rawtypes")
	@Override
	protected String searchData() throws AppException {
		OaLeaveSearchCondition searchCondition = (OaLeaveSearchCondition) this
				.getSearch();
		if ("deal".equals(this.opMenu)) {
			List<Task> tasks = this.getProcessDealService().findTodoTasks(
					User.getUserId(), "leave");
			String inTaskIds = this.getProcessDealService().findTodoTasksId(
					tasks);
			searchCondition.setInTaskIds(inTaskIds);
			String ret = super.searchData();
			List list = (List) this
					.getAttribute(WebConstants.SEARCH_RESULT_DATA);
			if (list != null && tasks != null) {
				for (int i = 0; i < list.size(); i++) {
					OaLeaveView leave = (OaLeaveView) list.get(i);
					for (Task task : tasks) {
						if (leave.getProcessInstanceId().equals(
								task.getProcessInstanceId())) {
							leave.setTaskId(task.getId());
							leave.setTaskName(task.getName());
						}
					}
				}
			}
			return ret;
		} else if ("all".equals(this.opMenu)) {
			searchCondition.setStatus("0");
		} else {
			searchCondition.setUserId(User.getUserId());
		}
		return super.searchData();
	}

	@Override
	protected void initOtherData(boolean isNew) {
		if (isNew) {
			try {
				BeanAccessUtil.copyProperty(po, "userId", User.getUserId());
				BeanAccessUtil.copyProperty(po, "orgId", User.getUserOrgId());
				BeanAccessUtil.copyProperty(po, "status", 0);
				BeanAccessUtil.copyProperty(po, "statusInfo", "未提交申请");
				BeanAccessUtil.copyProperty(po, "applyTime", new Date());
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}

	public String getOpMenu() {
		return opMenu;
	}

	public void setOpMenu(String opMenu) {
		this.opMenu = opMenu;
	}

}
